package com.example.douyin.login;

import androidx.lifecycle.MutableLiveData;

import com.example.lib_base_mvvm.baseMVVM.BaseRepo.BaseRepo;
import com.example.lib_base_mvvm.entity.UserEntity;
import com.hyphenate.EMCallBack;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class LoginRepo extends BaseRepo {

    public void login(RequestBody body, MutableLiveData<UserEntity> success,MutableLiveData<String> fail){
        apiServer.login(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserEntity entity) {
                        success.postValue(entity);
                    }

                    @Override
                    public void onError(Throwable e) {
                        fail.postValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void loginIM(String username,String password,MutableLiveData<String> success,MutableLiveData<String> fail){
        emClient.logout(true);//防止有用户未退出，新用户不能登录
        emClient.login(username, password, new EMCallBack() {
            @Override
            public void onSuccess() {
                success.postValue("登录成功");
            }

            @Override
            public void onError(int code, String error) {
                fail.postValue("登录失败"+error);
            }
        });
    }
}
